﻿@page "/lotteries/home"
@page "/lotteries"

@inject IDataCacheService _dataCacheService
@inject IHttpService _httpService
@inject IPopupService PopupService
@inject IUserService _userService
@inject NavigationManager NavigationManager


<CnGalWebSite.Shared.MasaComponent.Shared.Cards.TitleTip Title="抽奖板块" Description="快来参加抽奖吧" />

<div class="lottery-home-page">
        @if (isReady)
        {
            @foreach (var item in Model)
            {
            <CnGalWebSite.Shared.Component.Lotteries.LotteryCardView Model="item" OnParticipateInLottery="@(()=>OnParticipateInLottery(item))" />
            }
        }
        else
        {
            <CnGalWebSite.Components.Progressings.ProgressingCard Page/>
        }

</div>
<AuthorizeView>
                        <Authorized>
                          <CnGalWebSite.Shared.MasaComponent.PC.Entries.PlayedGames.EditUserSteamInforTip @ref="editUserSteamInforTip" />
    </Authorized>
                        
                    </AuthorizeView>
<CnGalWebSite.Components.Verifications.DeviceIdentification @ref="deviceIdentification" />

@code {


    private List<LotteryCardViewModel> Model = new List<LotteryCardViewModel>();

    bool isReady = false;
    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    CnGalWebSite.Components.Verifications.DeviceIdentification deviceIdentification;



    CnGalWebSite.Shared.MasaComponent.PC.Entries.PlayedGames.EditUserSteamInforTip editUserSteamInforTip;

    public void OnEditSteam()
    {
        editUserSteamInforTip?.Refresh(true);
    }

    protected override async Task OnInitializedAsync()
    {

        try
        {
            Model = await _httpService.GetAsync<List<LotteryCardViewModel>>("api/lotteries/GetLotteryCards");
            isReady = true;
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取抽奖主页信息列表失败");
        }
    }



    public async Task OnParticipateInLottery(LotteryCardViewModel item)
    {
        try
        {
            // 判断是否已经绑定Steam账号
            if (item.ConditionType == LotteryConditionType.Wishlist)
            {
                var model = await _httpService.GetAsync<EditUserDataViewModel>("api/space/EditUserData");
                if (model.SteamIds.Count == 0)
                {
                    OnEditSteam();
                    return;
                }
            }

            var obj = await _httpService.PostAsync<ParticipateInLotteryModel, Result>("api/lotteries/ParticipateInLottery", new ParticipateInLotteryModel
                {
                    Id = item.Id,
                    Identification = await deviceIdentification.GetDeviceIdentificationModel()
                });
            if (obj.Successful)
            {
                await PopupService.ToastAsync("参与抽奖成功", AlertTypes.Success);
                item.IsParticipated = true;
                item.ShowError = false;
                StateHasChanged();
            }
            else
            {
                if (obj.Error.StartsWith("获取愿望单失败"))
                {
                    item.ShowError = true;
                    StateHasChanged();
                }
                await PopupService.ToastErrorAsync("参与抽奖失败", obj.Error);
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "参与抽奖失败");
        }
    }

    private void OnCreateLottery()
    {
        NavigationManager.NavigateTo("/lotteries/createlottery");

    }
}
